package pl.edu.agh.uddiProxy.client;

import java.util.Collections;
import java.util.List;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public abstract class QueryHorizontalPanel extends HorizontalPanel {
	protected List<String> list;
	protected ListBox nameListBox;
	protected HorizontalPanel operatorPanel;
	protected ListBox logicalOperatorListBox;
	protected TextBox valueTextBox = new TextBox();
	protected HorizontalPanel unitPanel;
	
	public QueryHorizontalPanel(List<String> list, String[] operators, final ClickListener searchListener) {
		this.list = list;
		
		setSpacing(3);
		
		Collections.sort(list);
		nameListBox = new ListBox(false);
		for (String key : list) {
			nameListBox.addItem(key);
		}
		
		operatorPanel = new HorizontalPanel();
		unitPanel = new HorizontalPanel();
		
		logicalOperatorListBox = new ListBox(false);
		
		for (String op : operators) {
			logicalOperatorListBox.addItem(op);
		}
		
		add(nameListBox);
		add(operatorPanel);
		add(valueTextBox);
		add(unitPanel);
		add(logicalOperatorListBox);
		
		valueTextBox.addKeyboardListener(new KeyboardListenerAdapter() {
			@Override
			public void onKeyDown(Widget sender, char keyCode, int modifiers) {
				if (keyCode == KEY_ENTER) {
					searchListener.onClick(null);
				}
				super.onKeyDown(sender, keyCode, modifiers);
			}
		});
	}
	
	public ListBox getLogicalOperatorListBox() {
		return logicalOperatorListBox;
	}
	
}
